
#include "MySQLClient.h"
#include <cppconn/statement.h>  // 对应的实际头文件路径


MySQLClient::MySQLClient(const std::string& host, const std::string& user, const std::string& password, const std::string& database) {
    try {
        driver = sql::mysql::get_mysql_driver_instance();
        con = driver->connect(host, user, password);
        con->setSchema(database);
    } catch (sql::SQLException &e) {
        std::cerr << "SQL Exception: " << e.what() << std::endl;
        std::cerr << "MySQL error code: " << e.getErrorCode() << std::endl;
        std::cerr << "SQLState: " << e.getSQLState() << std::endl;
        con = nullptr; // Indicate connection failure
    } catch (std::exception &e) {
        std::cerr << "Exception: " << e.what() << std::endl;
        con = nullptr; // Indicate connection failure
    }
}

MySQLClient::~MySQLClient() {
    delete con;
}

// Function to execute an INSERT, UPDATE or DELETE query
void MySQLClient::executeQuery(const std::string& query) {
    sql::Statement* stmt = con->createStatement();
    stmt->execute(query);
    delete stmt;
}

// Function to execute a SELECT query and process results
void MySQLClient::executeSelectQuery(const std::string& query) {
    sql::Statement* stmt = con->createStatement();
    sql::ResultSet* res = stmt->executeQuery(query);

    while (res->next()) {
        std::string id = std::to_string(res->getInt("id"));
        std::string pass = res->getString("pass");
        std::string status = res->getString("status");
        std::string user = res->getString("user");

        std::string result = "ID: " + id + ", Password: " + pass + ", Status: " + status + ", User: " + user;
        std::cout << result << std::endl;
    }

    delete res;
    delete stmt;
}
/**
 * @brief 
 * 
 * @param username 
 * @param password 
 * @return int 0:找到了，密码用户名正确   -1：未找到用户 -2：密码错误
 */
int MySQLClient::loginSelectQuery(const std::string username , const std::string password ) 
{
    std::string query = "SELECT username, password FROM user WHERE username = '" + username + "';";
    sql::Statement* stmt = con->createStatement();
    sql::ResultSet* res = stmt->executeQuery(query);

    if (res->next()) {
        std::cout << "Username: " << res->getString("username") << std::endl;
        std::cout << "Password: " << res->getString("password") << std::endl;
        if( res->getString("password") != password )
        {
            return 2;
        }
    } else {
        std::cout << "No user found with username." << std::endl;
        return 1;
    }
    delete res;
    delete stmt;
    return 0;
}


bool MySQLClient::connect( )
{
    if( con != nullptr )
    {
        return true;
    }
    return false;
}

/*
int main() {
// Replace with your database credentials
MySQLClient dbClient("tcp://127.0.0.1:3306", "user", "password", "your_database");

// Example INSERT query
dbClient.executeQuery("INSERT INTO your_table (id, pass, status, user) VALUES (1, 'password', 'active', 'username')");

// Example UPDATE query
dbClient.executeQuery("UPDATE your_table SET pass = 'newpassword' WHERE id = 1");

// Example DELETE query
dbClient.executeQuery("DELETE FROM your_table WHERE id = 1");

// Example SELECT query
dbClient.executeSelectQuery("SELECT id, pass, status, user FROM your_table WHERE condition = 'value'");

return 0;
}
*/